package com.yeyks.config.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yeyks.common.em.AppEnum;
import com.yeyks.common.utils.ObjectConvertUtil;
import com.yeyks.config.dal.dao.AppInfoMapper;
import com.yeyks.config.dal.domain.AppInfo;
import com.yeyks.config.dto.AppInfoDTO;
import com.yeyks.config.service.AppInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * app信息 服务实现类
 * </p>
 *
 * @author yun
 * @since 2019-06-25
 */
@Slf4j
@Service
public class AppInfoServiceImpl extends ServiceImpl<AppInfoMapper, AppInfo> implements AppInfoService {


    private Map<String, String> forcedToNewMap = new HashMap<>();
    private List<String> noCheckList = CollectionUtil.newArrayList("JS", "Android");
    private List<String> checkList = CollectionUtil.newArrayList("iOS");

    @PostConstruct
    public void init() {
        for (String type : checkList) {
            AppInfo forcedToNewAppInfo = getForcedToNewAppInfo(type);
            String version = null;
            if (null != forcedToNewAppInfo) {
                version = forcedToNewAppInfo.getVersion();
            }
            forcedToNewMap.put(type, version);
        }
    }

    @Override
    public void refreshForcedVersion() {
        init();
    }

    //判断版本是否需要强制更新
    public Boolean getForcedToNewFlag(String type, String version) {
        if (StrUtil.compareVersion(forcedToNewMap.get(type), version) > 0) {
            return true;
        }
        return false;
    }

    @Override
    public Boolean checkNeedForcedUpdate(String osType, String osVersion) {
        if (StrUtil.isBlank(osType)) {
            return true;
        }else if (CollectionUtil.contains(checkList, osType)) {
            if (getForcedToNewFlag(osType, osVersion)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 根据类型和版本信息获取更新信息
     *
     * @param type
     * @param version
     * @return
     */
    @Override
    public AppInfoDTO getAppInfoByType(String type, String version) {
        AppInfoDTO appInfoDTO = null;
        try {
            AppInfo appInfo = baseMapper.selectByType(type);
            if (appInfo == null) {
                log.info("通过" + type + "类型的没找到合适版本信息");
                return null;
            }
            appInfoDTO = ObjectConvertUtil.convertObject(appInfo, AppInfoDTO.class);
            //安卓
            if (AppEnum.Android.desc().equals(type)) {
                int androidVersion = appInfo.getVersionCode();
                int nowVersion = Integer.valueOf(version);
                if (nowVersion >= androidVersion) {
                    appInfoDTO.setForcedUpdate(0);
                } else {
                    appInfoDTO.setTitle("立即更新");
                }
            }
            //IOS
            if (AppEnum.iOS.desc().equals(type)) {
                if (StrUtil.compareVersion(version, appInfo.getVersion()) >= 0) {
                    appInfoDTO.setForcedUpdate(0);
                } else {
                    appInfoDTO.setTitle("立即更新");
                }
            }
            if (checkNeedForcedUpdate(type, version)) {
                appInfoDTO.setForcedUpdate(2);
            }
        } catch (Exception e) {
            log.info("获取版本信息失败{}", e.getLocalizedMessage());
            return appInfoDTO;
        }
        return appInfoDTO;
    }

    @Override
    public AppInfo getForcedToNewAppInfo(String type) {
        AppInfo forcedToNewAppInfo = baseMapper.getForcedToNewAppInfo(type);
        return forcedToNewAppInfo;
    }

}
